//
//  MeasurementViewController.m
//  iRMAP
//
//  Created by Ryan Enguero on 4/24/14.
//  Copyright (c) 2014 engueroryan. All rights reserved.
//  SHEET!

#import "MeasurementViewController.h"
#import "MeasurementCell.h"
#import "ParserClass.h"
#import "Devices.h"
#import "DevicesCurrentTemperature-PGS.h"
#import "DevicesCurrentTemperature-Platinum.h"
#import "DevicesCurrentTemperature-CO2Incubator.h"
#import "DevicesCurrentTemperature-EGS-G2.h"
#import "DevicesCurrentTemperature-OFA_IFA_IFC_INA.h"
#import "loadingView.h"
#import "User.h"
#import "PostClass.h"

#define REFRESH_HEADER_HEIGHT 52.0f
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)

@interface MeasurementViewController (){

    NSString *deviceType;
}

@end

int measureTest = 1;
int measureDropDownState = 0;

NSMutableArray *temporaryDataDate;
NSMutableArray *temporaryDataTime;
NSMutableArray *temporaryDataTemp;

@implementation MeasurementViewController
@synthesize sensorList;


- (id)initWithStyle:(UITableViewStyle)style {
  //  self = [super initWithStyle:style];
    self = [super init];
    if (self != nil) {
        [self setupStrings];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self != nil) {
        [self setupStrings];
    }
    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self setupStrings];
        [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:0.0/255.0 green:30.0/255.0 blue:66.0/255.0 alpha:1.0], NSForegroundColorAttributeName,nil] forState:UIControlStateNormal];
        [self.navigationController.navigationBar setTintColor:[UIColor colorWithRed:0.0/255.0 green:30.0/255.0 blue:66.0/255.0 alpha:1.0]];
    }
    return self;
}



- (void)viewWillAppear:(BOOL)animated{
    
    
    [self.tabBarController.tabBar setHidden:YES];
    
    NSArray *xib = [[NSBundle mainBundle] loadNibNamed:@"loadingView" owner:self options:nil];
    loadingView * loading = [xib objectAtIndex:0];
    
    [loading setFrame:CGRectMake(0, -100, 93, 47)];
    
    [loading.imgLoading2 setAlpha:1.0];
    [loading.imgLoading setAlpha:0.0];
    
    [self.view addSubview:loading];
    [self.tblMeasurement setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
//    dispatch_async(kBgQueue, ^{
//        
//        
//        
//        dispatch_async(dispatch_get_main_queue(), ^{
    
            [loading removeFromSuperview];
            [self.tblMeasurement setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
            
            
            
            deviceType = [[ParserClass sharedItems] equipmentType];
            
            
            
            
            if ([deviceType isEqualToString:@"ultf_gold"] || [deviceType isEqualToString:@"ultf_silver"]) {
                
                sensorList = [NSMutableArray arrayWithObjects:@"Chamber Temperature Sensor", @"Chamber Set Point",@"High Temperature Limit", @"Low Temperature Limit", @"NTC Ambient Sensor", @"NTC Condenser Sensor", @"NTC Heat Exchanger 1", @"NTC Heat Exchanger 2", @"AC Voltage", nil];
                
            }else if ([deviceType isEqualToString:@"ultf_platinum"]){
                
                sensorList = [NSMutableArray arrayWithObjects:@"Chamber Temperature Sensor", @"Chamber Set Point",@"High Temperature Limit", @"Low Temperature Limit", @"Ambient Sensor", @"Condenser Outlet Sensor", @"Heat Exchanger Temperature Sensor", @"Evap In Sensor", @"Evap Out Sensor", @"Battery Voltage", @"AC Voltage", @"High Stage", nil];
                
                
            }else if ([deviceType isEqualToString:@"co2_incubator"]){
                
                sensorList = [NSMutableArray arrayWithObjects:@"Temperature Set Point",@"Co2 Set Point", @"O2 Set Point", @"High Temperature Limit", @"Low Temperature Limit", @"High Co2 Limit", @"Low Co2 Limit", @"High O2 Limit", @"Low O2 Limit", @"Low Rh Limit", @"Temperature Sensor", @"Co2 Sensor",@"O2 Sensor", @"Rh Sensor", nil];
                
                
            }else if ([deviceType isEqualToString:@"ecs-g2"]){
                
                sensorList = [NSMutableArray arrayWithObjects:@"Temperature Sensor", @"Low Inflow Limit",@"High Downflow Limit", @"Low Downflow Limit", @"High Temperature Limit", @"Low Temperature Limit", @"High DC Voltage Limit", @"Low DC Voltage Limit", @"Inflow Sensor", @"Downflow Sensor", @"DC Voltage", nil];
                
                
            }else if ([deviceType isEqualToString:@"ofa"] || [deviceType isEqualToString:@"ifa"] || [deviceType isEqualToString:@"ifc"] || [deviceType isEqualToString:@"ina"]){
                
                sensorList = [NSMutableArray arrayWithObjects:@"Chamber Temperature Sensor", @"Chamber Set Point",@"High Temperature Limit", @"Low Temperature Limit", nil];
            }
            
            
            
            temporaryDataDate = [NSMutableArray arrayWithObjects:@"12/12/14",@"12/13/14",@"12/29/14",@"12/30/14",@"12/31/14", nil];
            
            temporaryDataTemp = [NSMutableArray arrayWithObjects:@"9:11am",@"10:30am",@"11:30am",@"12:00am",@"12:30am", nil];
            
            temporaryDataTime = [NSMutableArray arrayWithObjects:@"5.0",@"4.5",@"3.0",@"5.0",@"1.0", nil];
            
            self.lblSensor.text = [sensorList objectAtIndex:0];
            
            
            [self.tblMeasurement reloadData];
            
            
            
//        });
//    });
    
    
}


-(void)viewWillDisappear:(BOOL)animated{

    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
      [self addPullToRefreshHeader];
    
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.0/255.0 green:30.0/255.0 blue:66.0/255.0 alpha:1.0];
    
    self.lblSensor.layer.cornerRadius = 6;
    self.btnCancelProp.layer.cornerRadius = 6;
    self.btnDoneProp.layer.cornerRadius = 6;
    
    
    
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[[ParserClass sharedItems] sensorLogs] count];
    //[temporaryDataDate count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *simpleTableIdentifier = @"SimpleTableItem";
    
    MeasurementCell *cell = (MeasurementCell*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    
    
    
    if (cell == nil) {
        
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MeasurementCell" owner:simpleTableIdentifier options:nil];
        
        cell = [nib objectAtIndex:0];
    }
    
    Devices *devices = [[ParserClass sharedItems] sensorLogs][indexPath.row];
    
    if ([[devices deviceType] isEqualToString:@"ultf_silver"] || [[devices deviceType] isEqualToString:@"ultf_gold"]) {
        
        NSLog(@"gold silver");
        
        DevicesCurrentTemperature_PGS *devicesGoldSilver = [[ParserClass sharedItems] sensorLogs][indexPath.row];
        
        
        cell.lblDate.text = [NSString stringWithFormat:@"%@", [[ParserClass sharedItems] convertLocaltoGMT:[devicesGoldSilver occuranceTime]]];
        
        
        if ([self.lblSensor.text isEqualToString:@"Chamber Set Point"]) {
        
                cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesGoldSilver chamberSetPoint] floatValue]];
        
        }else if ([self.lblSensor.text isEqualToString:@"High Temperature Limit"]){
            
                cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesGoldSilver highTemperatureLimit] floatValue]];
            
        }else if ([self.lblSensor.text isEqualToString:@"Low Temperature Limit"]){
            
                cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesGoldSilver lowTemperatureLimit] floatValue]];
            
        }else if ([self.lblSensor.text isEqualToString:@"Chamber Temperature Sensor"]){
            
                cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesGoldSilver chamberTemperatureSensor] floatValue]];
            
        }else if ([self.lblSensor.text isEqualToString:@"NTC Ambient Sensor"]){
            
                cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesGoldSilver ntcAmbientSensor] floatValue]];
            
        }else if ([self.lblSensor.text isEqualToString:@"NTC Condenser Sensor"]){
                
                cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesGoldSilver ntcCondenserSensor] floatValue]];
            
        }else if ([self.lblSensor.text isEqualToString:@"NTC Heat Exchanger 1"]){
            
                cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesGoldSilver ntcHeatExchanger1] floatValue]];
            
        }else if ([self.lblSensor.text isEqualToString:@"NTC Heat Exchanger 2"]){
            
                cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesGoldSilver ntcHeatExchanger2] floatValue]];
            
        }else if ([self.lblSensor.text isEqualToString:@"AC Voltage"]){
            
                cell.lblTemp.text = [NSString stringWithFormat:@"%@",[devicesGoldSilver acVoltage]];
        }
        
    }else if ([[devices deviceType] isEqualToString:@"ultf_platinum"]){
    
        NSLog(@"platinum");
        
        DevicesCurrentTemperature_Platinum *devicesPlatinum = [[ParserClass sharedItems] sensorLogs][indexPath.row];
   
        cell.lblDate.text = [NSString stringWithFormat:@"%@", [[ParserClass sharedItems] convertLocaltoGMT:[devicesPlatinum occuranceTime]]];
        
        if ([self.lblSensor.text isEqualToString:@"Chamber Set Point"]) {
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesPlatinum chamberSetPoint] floatValue]];
        }else if ([self.lblSensor.text isEqualToString:@"High Temperature Limit"]){
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesPlatinum highTemperatureLimit] floatValue]];
        
        }else if ([self.lblSensor.text isEqualToString:@"Low Temperature Limit"]){
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesPlatinum lowTemperatureLimit] floatValue]];
            
        }else if ([self.lblSensor.text isEqualToString:@"Chamber Temperature Sensor"]){
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesPlatinum chamberTemperatureSensor] floatValue]];
            
        }else if ([self.lblSensor.text isEqualToString:@"Ambient Sensor"]){
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesPlatinum ambientSensor] floatValue]];
            
        }else if ([self.lblSensor.text isEqualToString:@"Condenser Outlet Sensor"]){
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesPlatinum condenserOutletSensor] floatValue]];
            
        }else if ([self.lblSensor.text isEqualToString:@"Heat Exchanger Temperature Sensor"]){
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesPlatinum heatExchangerTempSensor] floatValue]];
            
        }else if ([self.lblSensor.text isEqualToString:@"Evap In Sensor"]){
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesPlatinum evapInSensor] floatValue]];
            
        }else if ([self.lblSensor.text isEqualToString:@"Evap Out Sensor"]){
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesPlatinum evapOutSensor] floatValue]];
            
        }else if ([self.lblSensor.text isEqualToString:@"Battery Voltage"]){
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesPlatinum batteryVoltage] floatValue]];
            
        }else if ([self.lblSensor.text isEqualToString:@"AC Voltage"]){
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%@",[devicesPlatinum acVoltage]];
            
        }else if ([self.lblSensor.text isEqualToString:@"High Stage"]){
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%@",[devicesPlatinum highStage]];
            
        }
        //cell.lblTemp.text = [NSString stringWithFormat:@"%@°C",[devicesPlatinum chamberTemperatureSensor]];
        
    }else if ([[devices deviceType] isEqualToString:@"co2_incubator"]){
    
        NSLog(@"incubator");
        
        DevicesCurrentTemperature_CO2Incubator *devicesIncubator = [[ParserClass sharedItems] sensorLogs][indexPath.row];
        
        cell.lblDate.text = [NSString stringWithFormat:@"%@", [[ParserClass sharedItems] convertLocaltoGMT:[devicesIncubator occuranceTime]]];
        
//        occuranceTime,!temperatureSetPoint,!co2SetPoint,!o2SetPoint,!highTemperatureLimit,! lowTemperatureLimit,!highCo2Limit,!lowCo2Limit,!highO2Limit,!lowO2Limit,!lowRhLimit,! temperatureSensor,!co2Sensor,!o2Sensor,!rhSensor!
        
        if ([self.lblSensor.text isEqualToString:@"Temperature Set Point"]) {
        
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesIncubator temperatureSetPoint] floatValue]];
        
        } else if ([self.lblSensor.text isEqualToString:@"Co2 Set Point"]) {
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesIncubator co2SetPoint] floatValue]];
            
        }else if ([self.lblSensor.text isEqualToString:@"O2 Set Point"]) {
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesIncubator o2SetPoint] floatValue]];
            
        }else if ([self.lblSensor.text isEqualToString:@"High Temperature Limit"]) {
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesIncubator highTemperatureLimit] floatValue]];
            
        }else if ([self.lblSensor.text isEqualToString:@"Low Temperature Limit"]) {
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesIncubator lowTemperatureLimit] floatValue]];
            
        }else if ([self.lblSensor.text isEqualToString:@"High Co2 Limit"]) {
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesIncubator highCo2Limit] floatValue]];
            
        }else if ([self.lblSensor.text isEqualToString:@"Low Co2 Limit"]) {
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesIncubator lowCo2Limit] floatValue]];
            
        }else if ([self.lblSensor.text isEqualToString:@"High O2 Limit"]) {
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesIncubator highO2Limit] floatValue]];
            
        }else if ([self.lblSensor.text isEqualToString:@"Low O2 Limit"]) {
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesIncubator lowO2Limit] floatValue]];
            
        }else if ([self.lblSensor.text isEqualToString:@"Low Rh Limit"]) {
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesIncubator lowRhLimit] floatValue]];
            
        }else if ([self.lblSensor.text isEqualToString:@"Temperature Sensor"]) {
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesIncubator temperatureSensor] floatValue]];
            
        }else if ([self.lblSensor.text isEqualToString:@"Co2 Sensor"]) {
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesIncubator co2Sensor] floatValue]];
            
        }else if ([self.lblSensor.text isEqualToString:@"O2 Sensor"]) {
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesIncubator o2Sensor] floatValue]];
            
        }else if ([self.lblSensor.text isEqualToString:@"Rh Sensor"]) {
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesIncubator rhSensor] floatValue]];
            
        }

        //cell.lblTemp.text = [NSString stringWithFormat:@"%@°C",[devicesIncubator temperatureSensor]];
    
    }else if ([[devices deviceType] isEqualToString:@"ecs-g2"]){
    
        NSLog(@"Ecs g2");
        
        DevicesCurrentTemperature_EGS_G2 *devicesEGSG2 = [[ParserClass sharedItems] sensorLogs][indexPath.row];
        
        cell.lblDate.text = [NSString stringWithFormat:@"%@", [[ParserClass sharedItems] convertLocaltoGMT:[devicesEGSG2 occuranceTime]]];
        
        if ([self.lblSensor.text isEqualToString:@"Low Inflow Limit"]) {
        
             cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesEGSG2 lowInflowLimit] floatValue]];
        }else if ([self.lblSensor.text isEqualToString:@"High Downflow Limit"]) {
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesEGSG2 highDownflowLimit] floatValue]];
            
        }else if ([self.lblSensor.text isEqualToString:@"Low Downflow Limit"]) {
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesEGSG2 lowDownflowLimit] floatValue]];
        }else if ([self.lblSensor.text isEqualToString:@"High Temperature Limit"]) {
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesEGSG2 highTemperatureLimit] floatValue]];
        }else if ([self.lblSensor.text isEqualToString:@"Low Temperature Limit"]) {
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesEGSG2 lowTemperatureLimit] floatValue]];
        }else if ([self.lblSensor.text isEqualToString:@"High DC Voltage Limit"]) {
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesEGSG2 highDcVoltageLimit] floatValue]];
        }else if ([self.lblSensor.text isEqualToString:@"Low DC Voltage Limit"]) {
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesEGSG2 lowDcVoltageLimit] floatValue]];
        }else if ([self.lblSensor.text isEqualToString:@"Inflow Sensor"]) {
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesEGSG2 inflowsensor] floatValue]];
        }else if ([self.lblSensor.text isEqualToString:@"Downflow Sensor"]) {
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesEGSG2 downflowSensor] floatValue]];
        }else if ([self.lblSensor.text isEqualToString:@"Temperature Sensor"]) {
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesEGSG2 temperatureSensor] floatValue]];
        }else if ([self.lblSensor.text isEqualToString:@"DC Voltage"]) {
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%@",[devicesEGSG2 dcVoltage]];
        }
        
        
      //  cell.lblTemp.text = [NSString stringWithFormat:@"%@°C",[devicesEGSG2 temperatureSensor]];
    
    }else if ([[devices deviceType] isEqualToString:@"ofa"] || [[devices deviceType] isEqualToString:@"ifa"] || [[devices deviceType] isEqualToString:@"ifc"] || [[devices deviceType] isEqualToString:@"ina"]){
    
        NSLog(@"ofa ifa ifc ina");
        
        DevicesCurrentTemperature_OFA_IFA_IFC_INA *devicesOFAIFAIFCINA = [[ParserClass sharedItems] sensorLogs][indexPath.row];
        
        cell.lblDate.text = [NSString stringWithFormat:@"%@", [[ParserClass sharedItems] convertLocaltoGMT:[devicesOFAIFAIFCINA occuranceTime]]];
        
        if ([self.lblSensor.text isEqualToString:@"Chamber Set Point"]) {
        
        cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesOFAIFAIFCINA chamberSetPoint] floatValue]];
            
        }else if ([self.lblSensor.text isEqualToString:@"High Temperature Limit"]) {
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesOFAIFAIFCINA highTemperatureLimit] floatValue]];
            
        }else if ([self.lblSensor.text isEqualToString:@"Low Temperature Limit"]) {
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesOFAIFAIFCINA lowTemperatureLimit] floatValue]];
            
        }else if ([self.lblSensor.text isEqualToString:@"Chamber Temperature Sensor"]) {
            
            cell.lblTemp.text = [NSString stringWithFormat:@"%.02f°C",[[devicesOFAIFAIFCINA chamberTemperatureSensor] floatValue]];
            
            
        }



    
    }
    
//    [cell.lblDate setText:[temporaryDataDate objectAtIndex:indexPath.row]];
//    [cell.lblTime setText:[temporaryDataTime objectAtIndex:indexPath.row]];
//    [cell.lblTemp setText:[temporaryDataTemp objectAtIndex:indexPath.row]];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    return cell;
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSString *simpleTableIdentifier = @"SimpleTableItem";
    
    MeasurementCell *cell = (MeasurementCell*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    
    
    
    if (cell == nil) {
        
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MeasurementCell" owner:simpleTableIdentifier options:nil];
        
        cell = [nib objectAtIndex:0];
    }

    cell.lblDate.text = @"Date/Time";
    cell.lblTemp.text = @"Degrees";
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 57.0;
}

- (IBAction)btnSensor:(id)sender {
    
    [self animationToolBar];
}

- (IBAction)btnCancel:(id)sender {
    [UIView animateWithDuration:0.5 animations:^{
        
        [self.viewPickerDate setFrame:CGRectMake(0, 568, 320, 221)];
        [self.viewPickerDate setAlpha:0.0];
    }completion:^(BOOL finished){
        [self.viewPickerDate removeFromSuperview];
        measureTest = 1;
    }];
    
}

- (IBAction)btnDone:(id)sender {
    [UIView animateWithDuration:0.5 animations:^{
        
        [self.viewPickerDate setFrame:CGRectMake(0, 568, 320, 221)];
        [self.viewPickerDate setAlpha:0.0];
    }completion:^(BOOL finished){
        [self.viewPickerDate removeFromSuperview];
        
        NSString *sensorPick = [self pickerView:self.sensorPicker titleForRow:[self.sensorPicker selectedRowInComponent:0] forComponent:0 ];    
        
        self.lblSensor.text = sensorPick;
        [self.tableView reloadData];
        measureTest = 1;
    }];

    
}


-(void)animationToolBar{
    if (measureTest == 1) {
        
        [self.viewPickerDate setFrame:CGRectMake(0, 568, 320, 221)];
        
        [self.viewPickerDate setAlpha:0.0];
        
     //   [self.view addSubview:self.viewPickerDate];
        
        [self.view addSubview:self.viewPickerDate];
        [UIView animateWithDuration:0.5 animations:^{
            
            [self.viewPickerDate setFrame:CGRectMake(0, 350, 320, 221)];
            [self.viewPickerDate setAlpha:1.0];
        }completion:^(BOOL finished){
            
            
            measureTest = 2;
        }];
        
        //NSLog(@"test %i",test);
        
    }else if (measureTest == 2){
        
        [self.viewPickerDate setFrame:CGRectMake(0, 350, 320, 221)];
        [self.viewPickerDate setAlpha:1.0];
        
        [UIView animateWithDuration:0.5 animations:^{
            
            [self.viewPickerDate setFrame:CGRectMake(0, 568, 320, 221)];
            [self.viewPickerDate setAlpha:0.0];
        }completion:^(BOOL finished){
            [self.viewPickerDate removeFromSuperview];
            measureTest = 1;
        }];
        
        //NSLog(@"test %i",test);
    }
    
}

#pragma mark - Refresh Method

- (void)setupStrings{
    
    textPull = @"Pull down to refresh...";
    textRelease = @"Release to refresh...";
    textLoading = @"Loading...";
}

- (void)addPullToRefreshHeader {
    refreshHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0 - REFRESH_HEADER_HEIGHT, 320, REFRESH_HEADER_HEIGHT)];
    refreshHeaderView.backgroundColor = [UIColor clearColor];
    
    refreshLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, REFRESH_HEADER_HEIGHT)];
    refreshLabel.backgroundColor = [UIColor clearColor];
    refreshLabel.font = [UIFont boldSystemFontOfSize:12.0];
    refreshLabel.textAlignment = NSTextAlignmentCenter;
    
    refreshArrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow.png"]];
    refreshArrow.frame = CGRectMake(floorf((REFRESH_HEADER_HEIGHT - 27) / 2),
                                    (floorf(REFRESH_HEADER_HEIGHT - 44) / 2),
                                    27, 44);
    
    refreshSpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    refreshSpinner.frame = CGRectMake(floorf(floorf(REFRESH_HEADER_HEIGHT - 20) / 2), floorf((REFRESH_HEADER_HEIGHT - 20) / 2), 20, 20);
    refreshSpinner.hidesWhenStopped = YES;
    
    [refreshHeaderView addSubview:refreshLabel];
    [refreshHeaderView addSubview:refreshArrow];
    [refreshHeaderView addSubview:refreshSpinner];
    [self setupStrings];
    [self.tblMeasurement addSubview:refreshHeaderView];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    if (isLoading) return;
    isDragging = YES;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (isLoading) {
        // Update the content inset, good for section headers
        if (scrollView.contentOffset.y > 0)
            self.tblMeasurement.contentInset = UIEdgeInsetsZero;
        else if (scrollView.contentOffset.y >= -REFRESH_HEADER_HEIGHT)
            self.tblMeasurement.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (isDragging && scrollView.contentOffset.y < 0) {
        // Update the arrow direction and label
        [UIView animateWithDuration:0.25 animations:^{
            if (scrollView.contentOffset.y < -REFRESH_HEADER_HEIGHT) {
                // User is scrolling above the header
                refreshLabel.text = self.textRelease;
                [refreshArrow layer].transform = CATransform3DMakeRotation(M_PI, 0, 0, 1);
            } else {
                // User is scrolling somewhere within the header
                refreshLabel.text = self.textPull;
                [refreshArrow layer].transform = CATransform3DMakeRotation(M_PI * 2, 0, 0, 1);
            }
        }];
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (isLoading) return;
    isDragging = NO;
    if (scrollView.contentOffset.y <= -REFRESH_HEADER_HEIGHT) {
        // Released above the header
        [self startLoading];
    }
}

- (void)startLoading {
    isLoading = YES;
    
    // Show the header
    [UIView animateWithDuration:0.3 animations:^{
        self.tblMeasurement.contentInset = UIEdgeInsetsMake(REFRESH_HEADER_HEIGHT, 0, 0, 0);
        refreshLabel.text = self.textLoading;
        refreshArrow.hidden = YES;
        [refreshSpinner startAnimating];
    }];
    
    // Refresh action!
    [self refresh];
    [self refreshView];
}

- (void)stopLoading {
    isLoading = NO;
    
    // Hide the header
    [UIView animateWithDuration:0.3 animations:^{
        self.tblMeasurement.contentInset = UIEdgeInsetsZero;
        [refreshArrow layer].transform = CATransform3DMakeRotation(M_PI * 2, 0, 0, 1);
    }
                     completion:^(BOOL finished) {
                         [self performSelector:@selector(stopLoadingComplete)];
                     }];
}

- (void)stopLoadingComplete {
    // Reset the header
    refreshLabel.text = self.textPull;
    refreshArrow.hidden = NO;
    [refreshSpinner stopAnimating];
}

- (void)refresh {
    // This is just a demo. Override this method with your custom reload action.
    // Don't forget to call stopLoading at the end.
    
  //  dispatch_async(dispatch_get_main_queue(), ^{
        [self performSelector:@selector(stopLoading) withObject:nil afterDelay:2.0];
   // });
    
}

- (void)dealloc {
//    [refreshHeaderView release];
//    [refreshLabel release];
//    [refreshArrow release];
//    [refreshSpinner release];
//    [textPull release];
//    [textRelease release];
//    [textLoading release];
 //   [super dealloc];
}


//-(void)setMeasurementsData:(NSString *)measurementData{
//    
//    deviceType = measurementData;
//}


- (void)refreshView {
    //dispatch_async(dispatch_get_main_queue(), ^{
        [self performSelector:@selector(addItem) withObject:nil afterDelay:1.0];
    //});
    
}

- (void)addItem {
    // Add a new time
//    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
//    [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
//    NSString *now = [dateFormatter stringFromDate:[NSDate date]];
//    [items insertObject:[NSString stringWithFormat:@"%@", now] atIndex:0];
    
   // Devices *devices = [[ParserClass sharedItems] deviceDetails][self.pathIndex.row];
    User *user = [[ParserClass sharedItems] userdetails][0];
    ParserClass *parser = [ParserClass sharedItems];
        
        NSDictionary *json = [[PostClass sharedItems] getSensorLog:[user sessionID] andDomainID:[[ParserClass sharedItems] domainID] andDeviceID:[parser freezerID]];
        
        
        if ([[[json objectForKey:@"response"] objectForKey:@"status"] isEqualToString:@"success"]) {
                
                [parser sensorLogs:json];
        }

    
    
    NSLog(@"Refresh sensor log count = %i",[[[ParserClass sharedItems] sensorLogs] count]);
    [self.tblMeasurement reloadData];
    
    [self stopLoading];
}


#pragma PickerSetup;

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [sensorList count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return sensorList[row];
}


@end
